$(function () {

    /*一加载页面就加载数据*/
    displayLess(1, 5);

    /* 点击选择按钮执行查询事件 */
    $("#query_all").click(function () {
        displayLess(1, $("#query_page").bs_pagination('getOption', 'rowsPerPage'));
    })

    /** 展示数据 */
    function displayLess(pageNoStr, pageSizeStr) {
        var department = $("#sel_dempartment option:selected").val();
        var group = $("#sel_grouptable option:selected").val();
        var startDate = $("#startDate").val();
        var endDate = $("#endDate").val();
        var hospital = $("#hospital").val();
        var name = $("#name").val();
        var state = $("#state option:selected").val();
        /*执行ajax*/
        $.ajax({
            url: "../../foGroup/selectFoGroup",
            data: {
                department: department,
                group: group,
                startDate: startDate,
                endDate: endDate,
                hospital: hospital,
                name: name,
                state: state,
                pageNoStr: pageNoStr,
                pageSizeStr: pageSizeStr
            },
            type: "post",
            dataType: "json",
            success: function (map) {
                var htmlStr = "";
                var foGroupList = map.foGroupList;
                for (var i = 0; i < foGroupList.length; i++) {
                    htmlStr += "<tr>"
                    if( foGroupList[i].patientInformationSheet.diagnosis.length !=0 ){
                        var outpatientServiceNo = foGroupList[i].patientInformationSheet.diagnosis[0].outpatientServiceNo;
                        var hospitalNumber = foGroupList[i].patientInformationSheet.diagnosis[0].hospitalNumber;
                        var NumberNo =  "";
                        if (outpatientServiceNo != null) {
                            NumberNo += outpatientServiceNo;
                        }else if (hospitalNumber != null) {
                            NumberNo += hospitalNumber;
                        }
                        if (hospitalNumber != null) {
                            NumberNo += "/"+hospitalNumber;
                        }else if (outpatientServiceNo != null) {
                            NumberNo += "/"+outpatientServiceNo;
                        }
                        htmlStr += "<td>" + NumberNo + "</td>"
                    }else{
                        htmlStr += "<td></td>"
                    }
                    if( foGroupList[i].grouptable != null ){
                        htmlStr += "<td>" + foGroupList[i].grouptable.gname + "</td>"
                    }else{
                        htmlStr += "<td></td>"
                    }

                    htmlStr += "<td>" + foGroupList[i].patientInformationSheet.patientName + "</td>"
                    var gender = foGroupList[i].patientInformationSheet.gender;
                    if (gender == 1) {
                        htmlStr += "<td>男</td>"
                    } else if (gender == 2) {
                        htmlStr += "<td>女</td>"
                    } else {
                        htmlStr += "<td></td>"
                    }
                    var age = foGroupList[i].patientInformationSheet.age;
                    if (age == null) {
                        htmlStr += "<td></td>"
                    } else {
                        htmlStr += "<td>" + age + "</td>"
                    }
                    htmlStr += "<td>" + renderTime(foGroupList[i].dateOfEntry) + "</td>"
                    if( foGroupList[i].grouptable != null ){
                        htmlStr += "<td>" + renderTime(foGroupList[i].grouptable.beginTime) + "</td>"
                    }else{
                        htmlStr += "<td></td>"
                    }
                    var stateId = foGroupList[i].grouptable.state;
                    if (stateId == 1) {
                        htmlStr += "<td>已随访</td>"
                    } else {
                        htmlStr += "<td>未随访</td>"
                    }
                    htmlStr += "</tr>"
                }
                //展示到页面上
                $("tbody").html(htmlStr);
                /*页面插件-分页展示*/
                //计算总页数
                var totalPages = 1;
                if (map.countList % pageSizeStr == 0) {
                    totalPages = map.countList / pageSizeStr;
                } else {
                    totalPages = parseInt(map.countList / pageSizeStr) + 1;
                }
                $("#query_page").bs_pagination({
                    currentPage: pageNoStr,	//当前页码
                    rowsPerPage: pageSizeStr,	//每页显示条数
                    totalRows: map.countList,	//总条数
                    totalPages: totalPages,//总页数
                    visiblePageLinks: 5,	//最多显示的卡片数
                    showGoToPage: true,		//是否显示跳转到几页
                    showRowsPerPage: true,	//是否显示每页显示条数
                    showRowsInfo: true,		//是否显示记录条数
                    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
                    onChangePage: function (event, pageObj) {
                        displayLess(pageObj.currentPage, pageObj.rowsPerPage);
                        pageObjIndexLess = pageObj.currentPage;
                    }
                });
            },
            error: function () {
                top.location.href = '404.jsp'
            }
        });
    }

    /**
     *转换日期格式问题
     * @param time
     * @returns {string}
     */
    function renderTime(date) {
        var dateee = new Date(date).toJSON();
        return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    }


})